<template>
	<custom-layout custom-class="custom-layout">
		
		<!-- 第二层 -->
        <wd-collapse v-model="collapseList" class="list">
	        <wd-collapse-item
	              v-for="(secondLevel,jIndex) in formData"
	              :key="secondLevel.id"
	              :title="secondLevel.stageName"
	              :name="`eng${secondLevel.id}`"
	            >
                <template #title="{ expanded, disabled, isFirst }">
                  <view class="header">
                    <text >{{ secondLevel.stageName }}</text>
                   
           
           

                  </view>
                 
                </template>
                         <!-- 第三层 -->
                <wd-collapse v-model="third" class="list">
	            	<wd-collapse-item
	            	  v-for="(thirdLevel,index) in secondLevel.children"
	            	  :key="thirdLevel.id"
	            	  :title="thirdLevel.stageName"
	            	  :name="`third${thirdLevel.id}`"
	            	>
                		<template #title="{ expanded, disabled, isFirst }">
                		  <view class="header-wrap">
                		    <text class="name">{{ thirdLevel.stageName }}</text>
						
                		    <div class="upload-date" v-if="thirdLevel.completeStatus === '1'">
                		      上传日期：{{JSON.parse(thirdLevel.fileUrl)[0].uploadTime }}
                		    </div>
                		  </view>
                		</template>
              			<template v-for="(file2, index2) in JSON.parse(thirdLevel.fileUrl)" :key="index2">
              			  <file-preview :file="file2"  />
              			</template>
			  			<wd-cell-group border>
			  				<wd-radio-group 
			  					v-model="thirdLevel.result" 
								inline 
								shape="dot"
								:prop="`[${jIndex}].children[${index}].result`"   
								:rules="[
								    { required: true, message: '请选择' },   
								  ]" >
								  <wd-radio value="2">同意</wd-radio>
								  <wd-radio value="3">驳回</wd-radio>

							</wd-radio-group>
						</wd-cell-group>
						
						
	          		</wd-collapse-item>
	        	</wd-collapse>

	    	</wd-collapse-item>
		</wd-collapse>
		
		<!-- 提交 -->
		<wd-button block size="large" custom-class="submit-button" @click="handleSubmit">提交</wd-button>
		
	</custom-layout>
</template>

<script setup>
	import {
		reactive,
		ref
	} from 'vue';
	import {
		onLoad
	} from '@dcloudio/uni-app'
	import {
		useDictionaryStore,
		useUserStore
	} from '@/store/index.js'
	import {
		engineeringStagePictureExamine,
	} from '@/api/engineering/detail.js'
	import FilePreview from '@/packageA/components/file-preview/file-preview.vue';

	// 字典仓库
	const dictionaryStore = useDictionaryStore()
	// 用户仓库
	const userStore = useUserStore()
	
	// 表单ref
	const formRef = ref()
	// 表单数据
	const formData = ref([])
	const collapseList = ref([])
	const third = ref([])
	onLoad((option)=>{
		formData.value=JSON.parse(decodeURIComponent(option.row))
		// 动态设置默认展开的项
  		collapseList.value = formData.value.map(item => `eng${item.id}`)
  		third.value = formData.value.flatMap(secondLevel => 
  		  secondLevel.children.map(thirdLevel => `third${thirdLevel.id}`)
  		)
		
		
		
	})
	// 处理提交
	const handleSubmit = async () => {
		let submitData = [];
  let hasEmptyResult = false; // 标记是否有未填写的项
  let emptyResultItems = []; // 存储未填写的项的名称

  // 遍历数据，检查是否有未填写的 result
  formData.value.forEach((secondLevel) => {
    secondLevel.children.forEach((thirdLevel) => {
      submitData.push({
        id: thirdLevel.id,
        engineeringId: thirdLevel.engineeringId,
        result: thirdLevel.result
      });

      // 如果 result 未填写，记录该项的名称
      if (!thirdLevel.result) {
        hasEmptyResult = true;
        emptyResultItems.push(thirdLevel.stageName); // 存储未填写的项的名称
      }
    });
  });



  // 如果有未填写的项，提示用户
  if (hasEmptyResult) {
    uni.showToast({
      title: `请审核：${emptyResultItems.join('、')}`,
      icon: 'none',
      duration: 3000
    });
    return; // 终止提交
  }
    console.log(submitData, 'submitData');
  
		
		
		uni.showLoading({
			mask: true
		})



		engineeringStagePictureExamine(submitData).then(()=>{
			uni.navigateBack(); // 返回上一页
			uni.$emit('refresh-examine'); // 触发刷新线索列表
		}).catch((error) => {
			console.log(error);
			
			setTimeout(() => {
				uni.showToast({
					title: '提交失败',
					icon: 'error'
				})
			})
		}).finally(() => {
			uni.hideLoading()
			
		})
		
	}
	
</script>

<style lang="scss" scoped>
	// 自定义布局
	:deep(.custom-layout) {
		background: #F8F8F8;
	}

	// 提交按钮
	:deep(.submit-button) {
		margin: 32rpx;
	}
	.cell-title {
		font-size: 30rpx;
		font-weight: bold;
		margin: 24rpx;
		padding-left: 10rpx;
		position: relative;
	}
	.cell-title:before {
		content: "";
		position: absolute;
		left: -2px;
		top: 15%;
		height: 70%;
		width: 6rpx;
		background-color: #6a9cff;
	}
</style>